<?php

include_once "./conf.php";

class RRDData {
  public function showDef() {
  }

  public function showName() {
  }

  public function showExpr() {
  }
}

class RRDCDef extends RRDData {
  public $vname = "";
  public $sign = "";
  public $var1 = NULL;
  public $var2 = NULL;
/* 
  function __construct($var_name, $var) {
    $this->vname = $var_name;
    $this->var1 = $var;
  }
*/
  function __construct($var_name, $sign, $var1, $var2) {
    $this->vname = $var_name;
    $this->sign = $sign;
    $this->var1 = $var1;
    $this->var2 = $var2;
  }

  public function add($var) {
    return new RRDCDef("", "+", $this, $var);
  }

  public function sub($var) {
    return new RRDCDef("", "-", $this, $var);
  }

  public function mul($var) {
    return new RRDCDef("", "*", $this, $var);
  }

  public function div($var) {
    return new RRDCDef("", "/", $this, $var);
  }

  public function showDef() {
    $expr = $this->showExpr();
    $vname = $this->vname;
    return "CDEF:'$vname'=$expr";
  }

  public function showName() {
    return "'$this->vname'";
  }

  public function showExpr() {
    if ($this->var2 != NULL) {
      $expr1 = $this->var1->showExpr();
      $expr2 = $this->var2->showExpr();
      $sign = $this->sign;
      return "$expr1,$expr2,$sign";
    } else
    if ($this->sign != "") {
      $expr1 = $this->var1->showExpr();
      $sign = $this->sign;
      return "$expr1,$sign";
    } else {
      if ($this->var1 != NULL) {
        return $this->var1->showExpr();
      } else {
        return "";
      }
    }
  }
}

class RRDDef extends RRDData {
  public $vname = "";
  public $rrdfile = "";
  public $dsname = "value";
  public $CF = "AVERAGE";

  function __construct($var_name, $rrd_filename) {
    $this->vname = $var_name;
    $this->rrdfile = $rrd_filename;
  }

  function __construct1($var_name, $rrd_filename, $rrd_dsname, $rrd_CF) {
    $this->vname = $var_name;
    $this->rrdfile = $rrd_filename;
    $this->dsname = $rrd_dsname;
    $this->CF = $rrd_CF;
  }

  public function showDef() {
    $vname = $this->vname;
    $rrdfile = $this->rrdfile;
    $dsname = $this->dsname;
    $CF = $this->CF;
    return "DEF:'$vname'=$rrdfile:$dsname:$CF";
  }

  public function showName() {
    return "'$this->vname'";
  }

  public function showExpr() {
    return "'$this->vname'";
  }
}

class RRDConst extends RRDData {
  public $const = "";

  function __construct($const) {
    $this->const = $const;
  }

  public function showDef() {
    return "";
  }

  public function showName() {
    return strval($this->const);
  }

  public function showExpr() {
    return strval($this->const);
  }
}


class RRDGraph {
  public $opts=array('start'=>'s', 'end'=>'e', 'step'=>'S', 'title'=>'t', 'vlabel'=>'v',
    'width'=>'w', 'height'=>'h', 'upper-limit'=>'u', 'lower-limit'=>'l', 'font'=>'n',
    'base'=>'b', 'imgformat'=>'f');

  public $vals=array("base"=>"1024");
  public $defs="";

  public function setOpt($key, $val) {
    $this->vals[$key] = $val;
  }

  public function genOpt() {
    $optlist = "";
    foreach ($this->opts as $key => $val)
      if (array_key_exists($key, $this->vals)) {
        $setval = $this->vals[$key];
        $optlist .= " -$val '$setval'";
      }
    return $optlist;
  }

  public function setDataDef($defs) {
    $this->defs = $defs;
  }

  public function setElems($elems) {
    $this->elems = $elems;
  }

  public function genCmd() {
    $command = RRDTOOL." graph - ";
    $command .= " ".$this->genOpt();
    $command .= " ".$this->defs;
    $command .= " ".$this->elems;
    return $command;
  }
}
?>
